<?php
/**
 * Created by PhpStorm.
 * User: gyccvvh
 * Date: 2018-06-13
 * Time: 16:04
 */
namespace app\merchant\controller;


use app\merchant\service\Merchant;
use app\merchant\service\Mermenu;
use think\Request;

class Basic extends App
{

    protected $merchant = null;
    protected $mer_service = null,$menu_service = null;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->mer_service = new Merchant();
        $ret = $this->mer_service->check_login();
        if($ret->getCode() != \app\merchant\define\Merchant::$Code_Mer_Success) {
            $this->error($ret->getMsg(), url('/merchant/login'));
        }
        $this->merchant = $ret->getData("merchant");
        $rule = strtolower($request->module().'/'.$request->controller().'/'.$request->action());
        $ret = $this->mer_service->check_auth($this->merchant, $rule);
        if($ret->getCode() == \app\merchant\define\Merchant::$Code_Mer_Error) {
            $this->error($ret->getMsg(),url('/merchant/login'));
        }
        $this->assign("merchant",$this->merchant);
        //构建菜单视图
        $this->menu_service = new Mermenu();
        $ret = $this->menu_service->get_auth_menu($this->merchant['id'],$rule);
        if($ret->getCode() == \app\merchant\define\Merchant::$Code_Mer_Error) {
            $this->error($ret->getMsg(),url('/merchant/login'));
        }
        $menu_tree = $ret->getData('tree');
        $this->assign("menu_tree",$menu_tree);

        //获取当前页面下按钮权限
        $ret = $this->menu_service->get_auth_button($this->merchant['id'],$rule);
        if($ret->getCode() == \app\merchant\define\Merchant::$Code_Mer_Error) {
            $this->error($ret->getMsg(),url("/merchant/login"));
        }
        $buttons = $ret->getData('buttons');
        $this->assign("buttons",$buttons);
    }

}